package txunda.com.decoratemaster.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.mob.MobSDK;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import txunda.com.decorate.tools.ListUtils;
import txunda.com.decorate.tools.PreferencesUtils;
import txunda.com.decorate.tools.Settings;
import txunda.com.decorate.tools.ToolKit;

public class MyApplication extends Application {

    private final String PREF_USERINFO = "user_info";
    // 用户信息
    private static MyApplication application = null;
    private Map<String, String> userInfo;

    @Override
    public void onCreate() {

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        MobSDK.init(this);
        application = this;
        initUserInfo();
        start();
        super.onCreate();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }

    public static String BASE_URL = "http://jian_travel.txunda.com/index.php/Api";


    /**
     * 设置全局变量
     */
    private void start() {
        // 获得屏幕宽度（像素）
        Settings.displayWidth = ToolKit.getScreenWidth(this);
        // 获得屏幕高度（像素）
        Settings.displayHeight = ToolKit.getScreenHeight(this);
        // 文件路径设置
//        Settings.cacheCompressPath = FileManager.getCompressFilePath();
//        // 创建路径
//        new File(Settings.cacheCompressPath).mkdirs();
    }


    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 设置用户信息
     */
    public void setUserInfo(Map<String, String> userInfo) {
        this.userInfo = userInfo;
        // 把用户信息存进Preferences
        // 将userInfo Map的所有key存到列表中
        List<String> keys = new ArrayList<>();
        // 迭代。。。获取所有key
        Iterator<String> iterator = this.userInfo.keySet().iterator();
        // 迭代遍历Key
        while (iterator.hasNext()) {
            String key = iterator.next();
            keys.add(key);
            String value = this.userInfo.get(key);
            // 将key和value保存到本地
            PreferencesUtils.putString(this, key, value);
        }
        // 将keys转成字符串(key1,key2...)
        PreferencesUtils.putString(this, PREF_USERINFO, ListUtils.join(keys));
    }

    /**
     * 获取用户信息
     *
     * @return Map
     */
    public Map<String, String> getUserInfo() {
        return userInfo;
    }

    /**
     * 初始化用户信息
     */
    private void initUserInfo() {
        userInfo = new HashMap<>();
        // 获取存在本地的用户信息(key)
        String keys = PreferencesUtils.getString(this, PREF_USERINFO);
        if (!TextUtils.isEmpty(keys)) {
            // 按照","分割keys
            String[] userInfos = keys.split(ListUtils.DEFAULT_JOIN_SEPARATOR);
            for (String key : userInfos) {// 获取用户信息，将其保存到Map中
                userInfo.put(key, PreferencesUtils.getString(this, key));
            }
        }
    }

    /**
     * 获取Application对象
     *
     * @return
     */
    public static Application getApplication() {
        return application;
    }
}
